import type { RoleRecord } from '@/DatabaseType/Role';
import type { PaginationConfig } from '@/types';
import type { RequestResult } from '@/utils/request';
import { ServiceProxy } from '@/constant';
import { request } from '@umijs/max';

interface GetRoleListParams {
  name?: string;
  znName?: string;
}

// 获取角色列表
export async function getRoleList(
  params: GetRoleListParams & PaginationConfig,
): Promise<
  RequestResult<{
    total: number;
    list: RoleRecord[];
  }>
> {
  return request(`${ServiceProxy.DefaultPrefix}/role`, {
    method: 'GET',
    params,
  });
}

// 获取角色记录
export async function getRole(
  roleId: number,
): Promise<RequestResult<RoleRecord>> {
  return request(`${ServiceProxy.DefaultPrefix}/role/${roleId}`, {
    method: 'GET',
  });
}

// 创建角色，返回创建的角色id
export async function addRole(
  data: Pick<RoleRecord, 'name' | 'znName'> & { permissionIds: number[] },
): Promise<RequestResult<number>> {
  return request(`${ServiceProxy.DefaultPrefix}/role`, {
    method: 'POST',
    data,
  });
}

// 更新角色信息
export async function updateRole(
  roleId: number,
  data: Pick<RoleRecord, 'name' | 'znName'> & { permissionIds: number[] },
): Promise<RequestResult<void>> {
  return request(`${ServiceProxy.DefaultPrefix}/role/${roleId}`, {
    method: 'PUT',
    data,
  });
}

// 删除角色信息
export async function deleteRole(roleId: number): Promise<RequestResult<void>> {
  return request(`${ServiceProxy.DefaultPrefix}/role/${roleId}`, {
    method: 'DELETE',
  });
}
